package cn.stylefeng.guns.modular.xlb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.xlb.entity.Xlb;
import cn.stylefeng.guns.modular.xlb.enums.XlbExceptionEnum;
import cn.stylefeng.guns.modular.xlb.mapper.XlbMapper;
import cn.stylefeng.guns.modular.xlb.pojo.request.XlbRequest;
import cn.stylefeng.guns.modular.xlb.service.XlbService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 线路表业务实现层
 *
 * @author zhu
 * @date 2021/10/06 11:08
 */
@Service
public class XlbServiceImpl extends ServiceImpl<XlbMapper, Xlb> implements XlbService {

	@Override
    public void add(XlbRequest xlbRequest) {
        Xlb xlb = new Xlb();
        BeanUtil.copyProperties(xlbRequest, xlb);
        this.save(xlb);
    }

    @Override
    public void del(XlbRequest xlbRequest) {
        Xlb xlb = this.queryXlb(xlbRequest);
        this.removeById(xlb.getId());
    }

    @Override
    public void edit(XlbRequest xlbRequest) {
        Xlb xlb = this.queryXlb(xlbRequest);
        BeanUtil.copyProperties(xlbRequest, xlb);
        this.updateById(xlb);
    }

    @Override
    public Xlb detail(XlbRequest xlbRequest) {
        return this.queryXlb(xlbRequest);
    }

    @Override
    public PageResult<Xlb> findPage(XlbRequest xlbRequest) {
        LambdaQueryWrapper<Xlb> wrapper = createWrapper(xlbRequest);
        Page<Xlb> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Xlb> findList(XlbRequest xlbRequest) {
        LambdaQueryWrapper<Xlb> wrapper = this.createWrapper(xlbRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author zhu
     * @date 2021/10/06 11:08
     */
    private Xlb queryXlb(XlbRequest xlbRequest) {
        Xlb xlb = this.getById(xlbRequest.getId());
        if (ObjectUtil.isEmpty(xlb)) {
            throw new BusinessException(XlbExceptionEnum.XLB_NOT_EXISTED);
        }
        return xlb;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhu
     * @date 2021/10/06 11:08
     */
    private LambdaQueryWrapper<Xlb> createWrapper(XlbRequest xlbRequest) {
        LambdaQueryWrapper<Xlb> queryWrapper = new LambdaQueryWrapper<>();

        Long id = xlbRequest.getId();
        String xlbh = xlbRequest.getXlbh();
        String xlmc = xlbRequest.getXlmc();
        String xlzt = xlbRequest.getXlzt();
        java.math.BigDecimal xlbl = xlbRequest.getXlbl();
        String sfcz = xlbRequest.getSfcz();
        String tjcz = xlbRequest.getTjcz();
        String zdcz = xlbRequest.getZdcz();
        java.math.BigDecimal lcdj = xlbRequest.getLcdj();
        Integer jpk = xlbRequest.getJpk();
        String xlgs = xlbRequest.getXlgs();
        String lssj = xlbRequest.getLssj();
        String xllx = xlbRequest.getXllx();
        String createTime = xlbRequest.getCreateTime();
        Long createUser = xlbRequest.getCreateUser();
        String updateTime = xlbRequest.getUpdateTime();
        Long updateUser = xlbRequest.getUpdateUser();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Xlb::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(xlbh), Xlb::getXlbh, xlbh);
        queryWrapper.like(ObjectUtil.isNotEmpty(xlmc), Xlb::getXlmc, xlmc);
        queryWrapper.like(ObjectUtil.isNotEmpty(xlzt), Xlb::getXlzt, xlzt);
        queryWrapper.eq(ObjectUtil.isNotNull(xlbl), Xlb::getXlbl, xlbl);
        queryWrapper.like(ObjectUtil.isNotEmpty(sfcz), Xlb::getSfcz, sfcz);
        queryWrapper.like(ObjectUtil.isNotEmpty(tjcz), Xlb::getTjcz, tjcz);
        queryWrapper.like(ObjectUtil.isNotEmpty(zdcz), Xlb::getZdcz, zdcz);
        queryWrapper.eq(ObjectUtil.isNotNull(lcdj), Xlb::getLcdj, lcdj);
        queryWrapper.eq(ObjectUtil.isNotNull(jpk), Xlb::getJpk, jpk);
        queryWrapper.like(ObjectUtil.isNotEmpty(xlgs), Xlb::getXlgs, xlgs);
        queryWrapper.like(ObjectUtil.isNotEmpty(lssj), Xlb::getLssj, lssj);
        queryWrapper.like(ObjectUtil.isNotEmpty(xllx), Xlb::getXllx, xllx);
        queryWrapper.eq(ObjectUtil.isNotNull(createTime), Xlb::getCreateTime, createTime);
        queryWrapper.eq(ObjectUtil.isNotNull(createUser), Xlb::getCreateUser, createUser);
        queryWrapper.eq(ObjectUtil.isNotNull(updateTime), Xlb::getUpdateTime, updateTime);
        queryWrapper.eq(ObjectUtil.isNotNull(updateUser), Xlb::getUpdateUser, updateUser);

        return queryWrapper;
    }

}